/*
 * WorldWindSelectListener.java
 *
 * Created on Aug 19, 2007, 5:45:50 PM
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oslo.ui.util;

import gov.nasa.worldwind.SelectEvent;
import gov.nasa.worldwind.SelectListener;
import gov.nasa.worldwind.WWIcon;
import java.util.List;
import org.oslo.domain.HotelInfo;
import org.oslo.sessioninfo.domain.SessionInfo;
import org.oslo.ui.HotelInfoDialog;
import org.oslo.ui.OSLOApp;
import org.oslo.ui.OSLOView;
import org.oslo.ui.SessionDialog;

/**
 *
 * @author nl24167
 */
public class WorldWindSelectListener implements SelectListener {
    
    public WorldWindSelectListener() {
    }
    
    private WWIcon lastToolTipIcon = null;
    
    public void selected(SelectEvent event) {
        
        
        if (event.getEventAction().equals(SelectEvent.HOVER)) {
            System.out.println("Hovering: " + event.getTopObject());
            if (event.getTopObject() == null) {
                if (lastToolTipIcon != null) {
                    lastToolTipIcon.setShowToolTip(false);
                }
            }
            
            if (event.getTopObject() instanceof HotelWWIcon) {
                                
                HotelWWIcon icon = (HotelWWIcon) event.getTopObject();
                
                StringBuilder builder = new StringBuilder();
                builder.append("Name: ").append(icon.getInfo().getName()).append('\n');
                
                icon.setToolTipText(builder.toString());
                icon.setShowToolTip(true);
                
                lastToolTipIcon = icon;
            }
        }
        
        if (event.getEventAction().equals(SelectEvent.LEFT_CLICK)) {
            System.out.println("Clicked: " + event.getTopObject());
            if (event.getTopObject() == null) {
                if (lastToolTipIcon != null) {
                    lastToolTipIcon.setShowToolTip(false);
                }
            }
            
            if (event.getTopObject() instanceof HotelWWIcon) {
                                
                HotelWWIcon icon = (HotelWWIcon) event.getTopObject();
                
                HotelInfo info = icon.getInfo();
                HotelInfoDialog dialog = new HotelInfoDialog(OSLOApp.getApplication().getMainFrame(), false, info);
                dialog.setVisible(true);
            } else {
                List<SessionInfo> sessions = OSLOView.getInstance().getSessions();
                if (sessions != null) {
                    SessionDialog dialog = new SessionDialog(OSLOView.getInstance().getFrame(), false);
                    dialog.setVisible(true);
                }
            }

          }
        
//        } else if (event.getEventAction().equals(SelectEvent.HOVER)) {
//            if (lastToolTipIcon != null) {
//                lastToolTipIcon.setShowToolTip(false);
//                this.lastToolTipIcon = null;
//                // DesktopApplication2Frame.this.worldWindowGLCanvas1.repaint();
//            }
//            
//            if (event.hasObjects()) {
//                if (event.getTopObject() instanceof WWIcon) {
//                    this.lastToolTipIcon = (WWIcon) event.getTopObject();
//                    lastToolTipIcon.setShowToolTip(true);
//                    //   DesktopApplication2Frame.this.worldWindowGLCanvas1.repaint();
//                }
//            }
//        } else if (event.getEventAction().equals(SelectEvent.ROLLOVER))  {
//            DesktopApplication2Frame.this.highlight(event.getTopObject());
//        }
    }
}
